package cn.sdhuijin.healthexam.control;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import cn.sdhuijin.healthexam.util.Constants;

public class ControlManager {

	private String propertiesPath;
	private Properties properties = new Properties();

	private List<IControl> controls = new ArrayList<IControl>();

	public void setPropertiesPath(String path) {
		this.propertiesPath = path;
	}

	public void addControl(IControl control) {
		this.controls.add(control);
	}

	private void setValues() {
		for (IControl ctrl : this.controls) {
			ctrl.setValue(properties);
		}
	}

	private void getValues() {
		for (IControl ctrl : this.controls) {
			ctrl.getValue(properties);
		}
	}

	public void toControl() {
		File dirFile = null;
		File file = null;
		try {
			dirFile = new File(Constants.PROPERTIES_PATH);
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			file = new File(this.propertiesPath);
			if (!file.exists()) {
				file.createNewFile();
			}
			properties.load(new FileReader(this.propertiesPath));
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.setValues();
	}

	public void toProperties() {
		this.getValues();
		try {
			properties.store(new FileWriter(this.propertiesPath), null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
